package com.xxx.springboot.service.impl;

import com.xxx.springboot.service.IndexService;
import io.redisearch.Schema;
import io.redisearch.client.Client;
import io.redisearch.client.IndexDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private Client client;

    public boolean createProductIndex() {
        Schema schema = new Schema()
                .addTextField("title", 5.0)   // 文本字段，权重5.0
                .addNumericField("price")     // 数值字段
                .addTagField("category")      // 标签字段
                .addGeoField("location");     // 地理字段

        IndexDefinition rule = new IndexDefinition(IndexDefinition.Type.HASH)
                .setPrefixes("product:");    // 自动索引以 product: 开头的键

        return client.createIndex(schema, Client.IndexOptions.defaultOptions().setDefinition(rule));
    }
}
